<template>
  <div>
    <div>
      <ele-page
        v-loading="loading"
        :formConfig="searchDialog"
        ref="eleTable"
        :operateList="operateList"
        @handleOperateEvent="handleOperateEvent"
        :column="column"
        :getList="getList"
        uniqueId="mesMaterialStationStorageTable"
      >
      </ele-page>
    </div>
    <ele-form-drawer
      class="dialog-pack"
      :isLoading="isLoading"
      v-model="formDataDialog"
      v-bind="formDescDialog"
      :request-fn="handleSubmit"
      :visible.sync="visible"
      :title="title"
    >
    </ele-form-drawer>
  </div>
</template>

<script>
import {
  operateList,
  searchConfig,
  formDescDialog,
  rowConfig,
  sendThis,
} from "./config";
import {
  getList,
  rowCreate,
  rowUpdate,
  rowDel,
  rowDetail,
  importData,
  getTypeCodeList,
} from "@/api/materialStation/mesMaterialStationStorage";
import uploadNoAuto from "@/components/upload-no-auto";
import url from "@/axios/url";
const { minioUrl } = url();
export default {
  name: "mesMaterialStationStorage",
  components: {
    uploadNoAuto,
  },
  mixins: [searchConfig],
  data() {
    return {
      title: null,
      visible: false,
      isLoading: false,
      loading: false,
      formDataDialog: {},
      formDescDialog, //新增修改框
      searchConfig,
      operateList,
      getList,
      isLoadingImport: false, // 导入数据loading
      importData: {}, //导入数据
      importVisible: false,
      // 列表页字段的绑定配置
      column: [
        {
          type: "selection",
        },
        {
          type: "index",
          label: $i18n.t("common.order"),
          fixed: "left",
          width: 70,
        },
        {
          prop: "stationCode",
          label: $i18n.t("mesMaterialStationStorage.stationCode"),
          fixed: "left",
          width: 160,
          showOverflowTooltip: true,
        },
        {
          prop: "stationName",
          label: $i18n.t("mesMaterialStationStorage.stationName"),
          fixed: "left",
          width: 200,
          showOverflowTooltip: true,
        },
        {
          prop: "typeCode",
          label: $i18n.t("mesMaterialStationStorage.typeCode"),
          width: 160,
          showOverflowTooltip: true,
          optionProp: {
            value: "typeCode",
            label: "typeName",
          },
          //远程获取
          option: async (data) => {
            const list = await getTypeCodeList();
            return list.data.result;
          },
        },
        {
          prop: "equipmentCode",
          label: $i18n.t("mesMaterialStationStorage.equipmentCode"),
          width: 160,
          showOverflowTooltip: true,
        },
        {
          prop: "equipmentName",
          label: $i18n.t("mesMaterialStationStorage.equipmentName"),
          width: 160,
          showOverflowTooltip: true,
        },
        {
          prop: "workshopName",
          label: $i18n.t("mesMaterialStationStorage.workshopName"),
          width: 160,
          showOverflowTooltip: true,
        },
        {
          prop: "lineName",
          label: $i18n.t("mesMaterialStationStorage.lineName"),
          width: 160,
          showOverflowTooltip: true,
        },
        {
          prop: "workCellName",
          label: $i18n.t("mesMaterialStationStorage.workCellName"),
          width: 160,
          showOverflowTooltip: true,
        },
        {
          prop: "materialCode",
          label: $i18n.t("mesMaterialStationStorage.materialCode"),
          width: 160,
          showOverflowTooltip: true,
        },
        {
          prop: "materialName",
          label: $i18n.t("mesMaterialStationStorage.materialName"),
          width: 160,
          showOverflowTooltip: true,
        },
        {
          prop: "volume",
          label: $i18n.t("mesMaterialStationStorage.volume"),
          width: 140,
          showOverflowTooltip: true,
        },
        {
          prop: "safetyStock",
          label: $i18n.t("mesMaterialStationStorage.safetyStock"),
          width: 140,
          showOverflowTooltip: true,
        },
        {
          prop: "inventoryQuantity",
          label: $i18n.t("mesMaterialStationStorage.inventoryQuantity"),
          width: 140,
          showOverflowTooltip: true,
        },
        // {
        //   prop: "availableQuantity",
        //   label: "可用数量",
        //   width: 140,
        //   showOverflowTooltip: true,
        // },
        // {
        //   prop: "frozenQuantity",
        //   label: "冻结数量",
        //   width: 140,
        //   showOverflowTooltip: true,
        // },
        {
          prop: "unit",
          label: $i18n.t("mesMaterialStationStorage.unit"),
          width: 140,
          showOverflowTooltip: true,
        },
        // {
        //   prop: "createByName",
        //   label: "创建人",
        // },
        // {
        //   prop: "createTime",
        //   label: "创建时间",
        //   width: 180,
        // },
        {
          prop: "updateByName",
          label: $i18n.t("common.updateByName"),
          width: 140,
          showOverflowTooltip: true,
        },
        {
          prop: "updateTime",
          label: $i18n.t("common.updateTime"),
          width: 160,
          showOverflowTooltip: true,
        },
        {
          prop: "operate",
          label: $i18n.t("common.operation"),
          width: 160,
          fixed: "right",
          render: (h, scope) => {
            return (
              <ele-row-operation
                scope={scope}
                rowConfig={rowConfig}
                onRowOperate={this.rowOperate}
              />
            );
          },
        },
      ],
    };
  },
  computed: {},
  mounted() {
    sendThis(this);
  },
  methods: {
    //表格顶部按钮操作事件
    handleOperateEvent(data) {
      this.title = data.name;
      const fnc = {
        add: () => {
          this.disableFields(false);
          this.formDataDialog = {};
          this.visible = true;
        },
        deleteBatch: () => {
          this.$confirm(
            $i18n.t("common.deleteBatch",{name:$i18n.t('mesMaterialStationStorage.name')}),
            $i18n.t("common.tip"),
            {
              confirmButtonText: $i18n.t("common.ok"),
              cancelButtonText: $i18n.t("common.cancel"),
              type: "warning",
            }
          ).then(() => {
            const ids = data.rows.map((val) => val.id).join(",");
            this.rowDel(ids);
          });
        },
        exportExcel: () => {
          var url = `${minioUrl}/mes-template/料站库存导入模版.xls`;
          // console.log("aaaaaaa=", url);
          window.open(url);
        },
        import: () => {
          this.importDialogData = {};
          this.importVisible = true;
        },
      };
      fnc[data.handleName]();
    },
    //表格行操作事件
    rowOperate({ handleName, name, row }) {
      this.title = name;
      const fnc = {
        edit: () => {
          this.disableFields(false);
          this.rowDetail(row.id);
          this.visible = true;
        },
        detail: () => {
          this.disableFields(true);
          this.rowDetail(row.id);
          this.visible = true;
        },
        delete: () => {
          this.$confirm(
            $i18n.t("common.deleteSingle",{name:$i18n.t('mesMaterialStationStorage.name')}),
            $i18n.t("common.tip"),
            {
              confirmButtonText: $i18n.t("common.ok"),
              cancelButtonText: $i18n.t("common.cancel"),
              type: "warning",
            }
          ).then(() => {
            this.rowDel(row.id);
          });
        },
      };
      fnc[handleName]();
    },
    doSubmit({ title, api, data }) {
      this.isLoading = true;
      api(data)
        .then((res) => {
          const message = res.data.message;
          this.$message.success(message || title);
          this.visible = false;
          this.refresh();
        })
        .finally(() => {
          this.isLoading = false;
        });
    },
    //弹框提交事件
    handleSubmit(data) {
      if (this.isLoading) return; // 判断状态
      const params = this.formDataDialog.id
        ? { title: $i18n.t("common.editSuccess"), api: rowUpdate, data }
        : { title: $i18n.t("common.addSuccess"), api: rowCreate, data };
      this.doSubmit(params);
    },
    //删除
    rowDel(data) {
      this.loading = true;
      rowDel(data)
        .then((res) => {
          const message = res.data.message;
          this.$message.success(message || $i18n.t("common.deleteSuccess"));
          this.refresh();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    disableFields(flag) {
      this.formDescDialog.disabled = flag;
      this.formDescDialog.isShowSubmitBtn = !flag;
    },
    rowDetail(id) {
      rowDetail(id).then((res) => {
        this.formDataDialog = res.data.result;
      });
    },
    //刷新表格数据
    refresh(isReset = true) {
      this.$refs.eleTable.refresh(isReset);
    },
    //数据文件上传
    upLoadHandleChange(file) {
      this.$set(this.importData, "file", file);
    },
  },
};
</script>
